package com.sihan.swvp.action.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.sys.PrivUserService;


@Controller
@RequestMapping("/priv/user")
public class PrivUserAction extends BaseAction{
	
	@Autowired
	PrivUserService privUserService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping(value="/getMenu")
	public List<Map<String,Object>> getMenu(@RequestParam String user,@RequestParam String node){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("user", user);
		params.put("node", node);
		params.put("loginuser", getLoginUser().getId());
		List<Map<String,Object>> list = privUserService.getMenuTree(params);
		for (Map<String, Object> map : list) {
			getCh(map,user);
		}
		return list;
	} 
	
	private void getCh(Map<String, Object> map,String user){
		if("0".equals(CommonUtil.o2string(map.get("leaf")))){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("user", user);
			params.put("node", map.get("id"));
			params.put("loginuser", getLoginUser().getId());
			List<Map<String,Object>> list = privUserService.getMenuTree(params);
			for (Map<String, Object> map2 : list) {
				getCh(map2,user);
			}
			map.put("children", list);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getRole")
	public List<Map<String,Object>> getRole(@RequestParam String user){
		List<Map<String,Object>> list = privUserService.getRole(user);
		return list;
	} 
	
	@ResponseBody
	@RequestMapping(value="/addPriv")
	public Object addPriv(@RequestParam String user,@RequestParam String ids){
		try {
			/*privUserService.deleteAllPrivMenu(user);
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("user", user);
			params.put("ids", ids);
			privUserService.addPriv(params);
			
			return successMsg("");*/
			
			privUserService.deleteAllPrivMenu(user);
			String[] idsarray = ids.split(",");
//			privRoleService.deleteAllPriv(role);
			if(ids.indexOf(",") > 0){
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("user", user);
				params.put("ids", idsarray);
				privUserService.addPriv(params);
//				privRoleService.addPriv(params);
			}
			return successMsg("");
		} catch (Exception e) {
			// TODO: handle exception
			return failMsg("服务器异常");
		}
	} 
	
	@ResponseBody
	@RequestMapping(value="/addPrivRole")
	public Object addPrivRole(@RequestParam String user,@RequestParam String roles){
		try {
			String[] idsarray = roles.split(",");
			privUserService.deleteAllPrivRole(user);
			if(!CommonUtil.isEmptyString(idsarray[0])){
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("user", user);
				params.put("roles", idsarray);
				privUserService.addPrivRole(params);
				logService.addUserLog("权限分配", "权限", getLoginUser().getId(), "分配权限："+roles,getRemoteAddr(),1,1);
			}
			return successMsg("");
		} catch (Exception e) {
			// TODO: handle exception
			logService.addUserLog("权限分配", "权限", getLoginUser().getId(), "分配权限："+roles,getRemoteAddr(),0,1);
			return failMsg("服务器异常");
		}
	} 
}
